require "Common/define"
require "Logic/Config"
require "Common/functions"

FriendInfoItem = {};
HelperFunc.SetUIEnv(FriendInfoItem)
local this = FriendInfoItem;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;
this.img_Icon = nil;
this.txt_Name = nil;
this.txt_Level = nil;
this.btn_Chat = nil;
this.btn_DelFri = nil;
this.btn_ApplyFri = nil;
this.btn_RefuseFri = nil;
this.btn_AddFriend = nil;
this.btn_GetLove = nil;
this.btn_PushLove = nil;
this.btn_FriendCompeteBattle =nil;--切磋按钮
this.Btn_ShowFriendInfo = nil
this.txt_Online = nil;
this.txt_FightPower = nil
this.friend_data = nil;
this.chapterName = nil

function FriendInfoItem.Init()
    local name, packageName = getUIName(PanelNames.FriendInfoItem);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
end

function FriendInfoItem.onCreate(obj)
    obj.visible = (false);
end

function FriendInfoItem.initData(obj, data)
    img_Icon = obj:GetChild("Head"):GetChild("Icon")
    txt_Level = obj:GetChild("Level")
    txt_Name = obj:GetChild("Name")
    btn_Chat = obj:GetChild("btn_Chat");
    btn_DelFri = obj:GetChild("btn_DelFri");
    btn_ApplyFri = obj:GetChild("btn_ApplyFri");
    btn_RefuseFri = obj:GetChild("btn_RefuseFri");
    btn_AddFriend = obj:GetChild("btn_AddFriend");
    btn_GetLove = obj:GetChild("btn_GetLove")
    btn_PushLove = obj:GetChild("btn_PushLove")
    btn_FriendCompeteBattle = obj:GetChild("btn_FriendCompeteBattle")
    this.Btn_ShowFriendInfo = obj:GetChild("Btn_ShowFriendInfo")
    txt_FightPower = obj:GetChild("fightpower")
    txt_Online = obj:GetChild("online")
    friendController = obj:GetController("friend")
    getLoveController = obj:GetController("getLove")
    pushLoveController = obj:GetController("pushLove")
    chapterName = obj:GetChild("currentLevel")
    this.province = obj:GetChild("province")
    local headframe = obj:GetChild("headframe")
    local ctrlOnline = obj:GetController("online")

    btn_ApplyFri.onClick:Clear();
    btn_RefuseFri.onClick:Clear();
    btn_AddFriend.onClick:Clear();
    btn_DelFri.onClick:Clear();
    btn_Chat.onClick:Clear();
    btn_GetLove.onClick:Clear();
    btn_PushLove.onClick:Clear();
    btn_FriendCompeteBattle.onClick:Clear();
    this.Btn_ShowFriendInfo.onClick:Clear()

    btn_ApplyFri.onClick:Add(function()
        this.onClickApplyFriend(data["partnerId"])
    end);
    btn_RefuseFri.onClick:Add(function()
        this.onClickRefuseFriend(data["partnerId"])
    end);
    btn_AddFriend.onClick:Add(function()
        this.onClickAddFriend(data["partnerId"])
    end);
    btn_DelFri.onClick:Add(function()
        this.onClickDelFriend(data["partnerId"])
    end);
    btn_Chat.onClick:Add(function()
        this.onClickChat(data["pic"], data["partnerId"], data["name"])
    end);
    btn_GetLove.onClick:Add(function()
        this.onClickGetLove(data["partnerId"], data["getLoveState"], getLoveController)
    end);
    btn_PushLove.onClick:Add(function()
        this.onClickPushLove(data["partnerId"], data["getLoveState"], pushLoveController)
    end);
    this.Btn_ShowFriendInfo.onClick:Add(function()
        this.onClickShowFriendInfo(data["partnerId"])
    end);
    btn_FriendCompeteBattle.onClick:Add(function ()
        this.onOpenFriendCompeteBattle(data["partnerId"])
    end);
    friendController.selectedIndex = 0

    UIRedDots.addViewListen(PanelNames.UIFriend, data.partnerId .. "btn_Chat", btn_Chat, Config.RedDotsType.PrivateChat, data.partnerId)

    local cfg = dataTable.getDataTableByStringId("t_skin", data.pic)
    if cfg == nil then
        logError("找不到t_hero  id:" .. tostring(data.pic))
        img_Icon.visible = false
        txt_Level.visible = false
        txt_Name.visible = false
        return
    end
    if data.dungeonId then
        local friendCur = G.dataTable["t_stage"][tostring(data.dungeonId)];
        if friendCur then
            chapterName.text = string.format(getLanguage("UIFriend_CurrentLevel"),friendCur.f_ChapterId .. "-" .. friendCur.f_StageNum)
        end
    end
    img_Icon.visible = true
    img_Icon.url = "ui://ICONAtlas/" .. cfg.f_HeroIcon
    headframe.url = "ui://ICONAtlas/" .. data.picFrame
    if data["level"] ~= nil and data["level"] > 0 then
        txt_Level.visible = (true);
        txt_Level.text = data["level"];
    end
    if data["maxPower"] then
        txt_FightPower.text = getNumString(data["maxPower"])
    end

    if data["name"] ~= nil then
        txt_Name.visible = (true);
        txt_Name.text = data["name"];
    end

    if data["offlineTime"] ~= nil then

        local offlineTime = tonumber(data["offlineTime"]);
        if offlineTime > 0 then
            ctrlOnline.selectedIndex = 1
            txt_Online.text = getTimeStrType2(offlineTime);
        else
            ctrlOnline.selectedIndex = 0
            txt_Online.text = getLanguage("Sys_Friend_Online");
        end
    else
        ctrlOnline.selectedIndex = 0
        txt_Online.text = getLanguage("Sys_Friend_Online");
    end
    this.province.text = data.lbs
    local getLoveState = data.getLoveState
    local sendLoveState = data.sendLoveState
    local state = data.state
    if getLoveState ~= nil and state == 1 then
        getLoveController.selectedIndex = 0
        if getLoveState == EnumConst.LoveStateEnum.NONE or getLoveState == EnumConst.LoveStateEnum.USED then
            btn_GetLove.visible = false
            btn_PushLove.visible = true;
            if sendLoveState ~= nil and state == 1 then
                pushLoveController.selectedIndex = 0
                if sendLoveState == EnumConst.LoveStateEnum.NONE then
                elseif sendLoveState == EnumConst.LoveStateEnum.NORMAL then
                    pushLoveController.selectedIndex = 1
                elseif sendLoveState == EnumConst.LoveStateEnum.USED then
                    pushLoveController.selectedIndex = 0
                end
            end
        elseif getLoveState == EnumConst.LoveStateEnum.NORMAL then
            getLoveController.selectedIndex = 1
            btn_PushLove.visible = false;
            btn_GetLove.visible = true
        else
        end
    end
    --if sendLoveState ~= nil and state == 1 then
    --    pushLoveController.selectedIndex = 0
    --    if sendLoveState == EnumConst.LoveStateEnum.NONE then
    --    elseif sendLoveState == EnumConst.LoveStateEnum.NORMAL then
    --        pushLoveController.selectedIndex = 1
    --    elseif sendLoveState == EnumConst.LoveStateEnum.USED then
    --        pushLoveController.selectedIndex = 0
    --    end
    --end

    if state == 1 then
        friendController.selectedIndex = 1
    elseif state == 2 then
        friendController.selectedIndex = 2
    elseif state == 3 then
        friendController.selectedIndex = 3
    elseif state == 4 then
        friendController.selectedIndex = 4
    end
end

function FriendInfoItem.onClickChat(pic, partnerId, name)
    closeUI(PanelNames.UIFriend);
    showUI(PanelNames.UIChatPanel);
    local data = {};
    data["head"] = pic;
    data["roleId"] = partnerId;
    data["roleName"] = name;
    UIChatPanel.PrivateChatMsgPort(data);
end

function FriendInfoItem.onClickApplyFriend(friendId)
    friendHandler.sendAgreeFriend(friendId);
end

function FriendInfoItem.onClickRefuseFriend(friendId)
    friendHandler.sendRefuseFriend(friendId);
end

function FriendInfoItem.onClickAddFriend(friendId)
    if logicMgr.FriendManager.IsFriend(friendId) then
        logicMgr.FriendManager.DeleteFriend(EnumConst.FriendType.recommend,friendId)
        local findFriend = logicMgr.FriendManager.GetFindFriend()
        if findFriend and findFriend.playerId == friendId then
            logicMgr.FriendManager.DeleteFriend(EnumConst.FriendType.find,friendId)
        end
        UISysTips.AddMsg(getLanguage("ErrorCode_140000")) --已是好友
        FireEvent(Config.EventType.Fresh_RecommendFriend);
        return
    end
    FireEvent(Config.EventType.FriendShowAddMessage, true, friendId)
end

function FriendInfoItem.onClickDelFriend(friendId)
    logicMgr.UIShowManager.ShowCommonConfirm(getLanguage("UIFriend_DeleteFriendTip"), nil, function()
        friendHandler.sendRemoveFriend(friendId);
    end)
end

function FriendInfoItem.onClickGetLove(partnerId, getLoveState, controller)
    if logicMgr.FriendManager.IsMaxFriendShipToday() then
        UISysTips.AddMsg("今日领取已达上限")--改成language
        return
    end
    if getLoveState ~= nil then
        getLoveState = EnumConst.LoveStateEnum.USED;
        controller.selectedIndex = 0
        local friendIds = {};
        table.insert(friendIds, partnerId);
        friendHandler.sendReceiveFriendship(friendIds);
        --UISysTips.AddMsg("友情点领取成功")
    end
end

function FriendInfoItem.onClickPushLove(partnerId, getLoveState, controller)
    if getLoveState ~= nil then
        getLoveState = EnumConst.LoveStateEnum.USED;
        controller.selectedIndex = 0
        local friendIds = {};
        table.insert(friendIds, partnerId);
        friendHandler.sendGiveFriendship(friendIds);
        --UISysTips.AddMsg("友情点赠送成功")
    end
end
function FriendInfoItem.ShowMessage(obj, message)
    local ctrl = obj:GetController("message")
    ctrl.selectedIndex = 1
    local txt = obj:GetChild("text")
    txt.text = message
end
function this.onClickShowFriendInfo(partnerId)
    roleInfoHandler.sendreqOtherRoleInfo(partnerId)
end

function FriendInfoItem.onOpenFriendCompeteBattle(friendId)
    friendHandler.ReqFriendCompeteInfo(friendId)
end

return FriendInfoItem;